/**
 * 
 */
package denaris.midlet;

import javax.microedition.lcdui.Graphics;

import denaris.midlet.DenarisGameCanvas.DisplayProperties;

/**
 * @author stkl23
 * 
 */
public class GameFadeScreen extends AbstractScreen {
	private static final int RECT_WIDTH = 32;

	private static final int RECT_HEIGHT = 32;

	private static final int EDGE_TOP = 0;

	private static final int EDGE_RIGHT = 1;

	private static final int EDGE_BOTTOM = 2;

	private static final int EDGE_LEFT = 3;

	private static GameFadeScreen instance;

	/**
	 * indicates the current edge - a rect has 32 edges. We can calculate the
	 * exact position within the rect from the index of the current edge and the
	 * count member
	 */
	private int edge = 0;

	private int color = 0x000000;

	private int delay = 50;

	public static GameFadeScreen getInstance() {
		if (instance == null) {
			instance = new GameFadeScreen();
		}
		return instance;
	}

	protected GameFadeScreen() {
	}

	public void initialize() {
		edge = 0;
		delay = 40;
		color = 0x000000;
	}

	/** draws a snake-rectangle at the given position into this screens buffer */
	private void drawRect(int x, int y, Graphics buffer) {
		// draw the sides
		int side = 0;
		int xa = 0;
		int ya = 0;
		int xe = 0;
		int ye = 0;
		int len = 0;
		// check which of the four sides we have to deal with
		side = edge % 4;
		len = RECT_WIDTH - (edge >> 1);
		switch (side) {
		case EDGE_TOP: {
			xa = edge >> 2;
			ya = edge >> 2;
			xe = xa + len;
			ye = ya;
			break;
		}
		case EDGE_RIGHT: {
			xa = RECT_WIDTH - (edge >> 2);
			xe = xa;
			ya = edge >> 2;
			ye = ya + len;
			break;
		}
		case EDGE_BOTTOM: {
			xa = edge >> 2;
			xe = xa + len;
			ya = RECT_HEIGHT - (edge >> 2);
			ye = ya;
			break;
		}
		case EDGE_LEFT: {
			xa = edge >> 2;
			xe = xa;
			ya = edge >> 2;
			ye = ya + len;
			break;
		}
		}
		// draw the line
		buffer.setColor(color);
		buffer.drawLine(xa + x, ya + y, xe + x, ye + y);
	}

	public void paintScreenToBuffer(Graphics g) {
		if (delay > 0) {
			GameScreen.getInstance().paintScreenToBuffer(g);
		} else {
			int x = 0;
			int y = DisplayProperties.GAME_TOP;

			while (y < DisplayProperties.GAME_TOP
					+ DisplayProperties.GAME_HEIGHT + RECT_HEIGHT) {
				x = 0;
				while (x < DisplayProperties.DISPLAY_WIDTH) {
					drawRect(x, y, g);
					x += RECT_WIDTH;
				}
				y += RECT_HEIGHT;
			}
		}
	}

	public void doFullRepaintToBuffer(Graphics g) {
		GameScreen.getInstance().doFullRepaintToBuffer(g);
		paintScreenToBuffer(g);
	}

	public void update() {
		if (delay > 0) {
			GameScreen.getInstance().getMap().update();
			delay--;
		} else {
			// next edge?
			if (edge <= (RECT_WIDTH << 1)) {
				// yes - we need a new edge
				edge += 1;
				// do not increment the color in order to have the screen fade
				// to black
//				color+=0x000102;
			} else {
				Denaris.getInstance().notifyFaded();
			}
		}
	}
}